package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Result;
import com.powernode.domain.Customer;
import com.powernode.domain.VisitLog;
import com.powernode.dto.BaseDto;
import com.powernode.service.VisitLogService;
import com.powernode.utils.WebMvcUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("visitLog")
@Api(tags = "拜访记录的管理接口")
public class VisitLogController {
    @Resource
    private VisitLogService visitLogService;

    @GetMapping("list.do")
    @ApiOperation("分页查询")
    public Result list(BaseDto baseDto) {
        Customer customer = WebMvcUtils.getCurrentCustomer();
        Integer id = customer.getId();
        PageInfo<VisitLog> pageInfo = visitLogService.selectAll(baseDto,id);
        return new Result(0, "查询成功", pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("add.do")
    @ApiOperation("添加拜访记录")
    public Result add(VisitLog visitLog) {
        try {
            Customer customer = WebMvcUtils.getCurrentCustomer();
            visitLog.setCreateTime(new Date());
            visitLog.setId(customer.getId());
            visitLog.setName(customer.getName());
            visitLog.setUserId(customer.getUserId());
            visitLog.setRealname(customer.getRealname());
            System.out.println(visitLog);
            visitLogService.insertSelective(visitLog);
            return Result.ADD_SUCCESS;
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }
}
